package com.thermal.thermal_inspection.controller;

import com.thermal.thermal_inspection.common.CommonResponse;
import com.thermal.thermal_inspection.entity.InspectionData;
import com.thermal.thermal_inspection.entity.InspectionDetail;
import com.thermal.thermal_inspection.service.BatchService;
import com.thermal.thermal_inspection.vo.BatchDetailVo;
import com.thermal.thermal_inspection.vo.BatchVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class BatchController {
    @Autowired
    BatchService batchService;

    /**
     * 更新批次信息
     * @param batchVO
     * @return
     */
    @PutMapping("/inspection/batch")
    public CommonResponse<String> updateBatch(@RequestBody BatchVO batchVO){
        if (batchService.existBatchName(batchVO.getNewBatchName()))
            return CommonResponse.createForError("批次名字重复！");
        Integer row = batchService.updateBatch(batchVO);
        if (row == 1)
            return CommonResponse.createForSuccess("修改批次信息成功！");
        return CommonResponse.createForError("修改批次信息失败！");
    }
    /**
     * 删除批次
     */
    @DeleteMapping("/inspection/batch/{batchId}")
    public CommonResponse<String> deleteBatch(@PathVariable("batchId") int batchId){
        Integer row = batchService.deleteBatch(batchId);
        if (row == 0)
            return CommonResponse.createForError("删除批次失败！");
        return CommonResponse.createForSuccess("删除批次成功！");
    }
    /**
     * 获取分配批次任务的模版
     */
    @GetMapping("/inspection/batchDetail/{batchId}")
    public CommonResponse<List<BatchDetailVo>> getInspectionOfBatch(@PathVariable("batchId") int batchId){
        List<BatchDetailVo> inspectionData = batchService.getInspectionDataByBatch(batchId);
        if (inspectionData == null)
            return CommonResponse.createForError("此批次内不存在任何分配任务！");
        if (inspectionData.isEmpty())
            return CommonResponse.createForError("此批次的模版点位任务为空！");
        return CommonResponse.createForSuccess(inspectionData);
    }

    /**
     * 修改批次里面的inspectionDetail的信息
     */
    @PutMapping("/inspection/batchDetail/{batchId}")
    public CommonResponse<String> updateBatchInspectionDetail(@PathVariable("batchId") int batchId,
                                                              @RequestBody List<InspectionDetail> inspectionDetails){
        String msg = batchService.updateInspectionDetailOfBatch(batchId,inspectionDetails);
        if (msg.equals("ERROR"))
            return CommonResponse.createForError("这个批次不存在！");
        if (msg.equals("ABNORMAL"))
            return CommonResponse.createForError("数据组不存在！");
        return CommonResponse.createForSuccess("更新未开始的巡检点位任务成功！");
    }
}
